package com.sc.es.controller.fe;

import com.sc.es.common.*;
import com.sc.es.dto.HouseDto;
import com.sc.es.dto.SupportAddressDto;
import com.sc.es.dto.UserDto;
import com.sc.es.entity.SupportAddress;
import com.sc.es.search.ISearchService;
import com.sc.es.service.IAddressService;
import com.sc.es.service.IHouseService;
import com.sc.es.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Max;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author sc
 * Created on  2018/2/18
 */
@Controller
public class HouseFEController {

    @Autowired
    private IHouseService houseService;
    @Autowired
    private IAddressService addressService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ISearchService searchService;

    @GetMapping("/rent/house")
    public String rentHousePage(@ModelAttribute RentSearchForm form,
                                Model model, HttpSession session,RedirectAttributes redirectAttributes){
        if(form.getCityEnName()==null){
            String cityNameInSession = (String) session.getAttribute("cityEnName");
            if(cityNameInSession==null){
                redirectAttributes.addAttribute("msg","must add city");
            }else {
                form.setCityEnName(cityNameInSession);
            }
        }else {
            session.setAttribute("cityEnName",form.getCityEnName());
        }
        HouseResult city = addressService.findCity(form.getCityEnName());
        if(!city.isSuccess()){
            redirectAttributes.addAttribute("msg","must_choose_city");
            return "redirect:/index";
        }

        ServiceResult allRegionsByCity = addressService.findAllRegionsByCity(form.getCityEnName());
        ServiceResult result = houseService.query(form);
        if(form.getRegionEnName()==null){
            form.setRegionEnName("*");
        }
        model.addAttribute("total",result.getTotal());
        model.addAttribute("houses",result.getResult());
        model.addAttribute("searchBody",form);
        model.addAttribute("regions",allRegionsByCity.getResult());
        model.addAttribute("currentCity",city.getResult());
        model.addAttribute("priceBlocks", RentValueBlock.PRICE_BLOCK);
        model.addAttribute("areaBlocks", RentValueBlock.AREA_BLOCK);
        model.addAttribute("currentPriceBlock",RentValueBlock.matchPrice(form.getPriceBlock()));
        model.addAttribute("currentAreaBlock",RentValueBlock.matchArea(form.getAreaBlock()));
        return "rent-list";
    }


    @GetMapping("/rent/house/show/{id}")
    public String show(@PathVariable(value = "id")Integer id,Model model){
        if(id==null){
            return "404";
        }
        HouseResult<HouseDto> one = houseService.findCompleteOne(id);
        if(!one.isSuccess()){
            return "404";
        }
        HouseDto houseDto = one.getResult();
        Map<SupportAddress.Level, SupportAddressDto> addressDtoMap = addressService.findCityAndRegion(houseDto.getCityEnName(), houseDto.getRegionEnName());
        HouseResult aggregateRes = searchService.aggregateDistrictHouse(houseDto.getCityEnName(), houseDto.getRegionEnName(), houseDto.getDistrict());
        SupportAddressDto city = addressDtoMap.get(SupportAddress.Level.CITY);
        SupportAddressDto region = addressDtoMap.get(SupportAddress.Level.REGION);
        HouseResult<UserDto> admin = userService.findById(houseDto.getAdminId());
        model.addAttribute("agent",admin.getResult());
        model.addAttribute("house",houseDto);
        model.addAttribute("city",city);
        model.addAttribute("region",region);
        model.addAttribute("houseCountInDistrict",aggregateRes.getResult());
        return "house-detail";
    }

    /**
     * search-as-you-type
     * @param prefix
     * @return
     */
    @GetMapping("/rent/house/autocomplete")
    @ResponseBody
    public ApiResponse autoComplete(String prefix){
        if(prefix.isEmpty()){
            return ApiResponse.ofStatus(ApiResponse.Status.BAD_REQUEST);
        }
        HouseResult houseResult = searchService.autoComplete(prefix);

        return ApiResponse.ofSuccess(houseResult.getResult());
    }
}
